package com.lc.luzhengtang.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.lc.luzhengtang.BaseApplication;
import com.lc.luzhengtang.R;
import com.lc.luzhengtang.adapter.WithDrawListAdapter;
import com.lc.luzhengtang.base.BaseActivity;
import com.lc.luzhengtang.bean.RecordBean;
import com.lc.luzhengtang.conn.ConnWithDrawRecord;
import com.lc.luzhengtang.util.LovelyToast;
import com.zcx.helper.http.AsyCallBack;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2017/7/21.
 */
public class WithDrawRecordActivity extends BaseActivity implements BaseQuickAdapter.RequestLoadMoreListener, SwipeRefreshLayout.OnRefreshListener {


    @BindView(R.id.img_news)
    ImageView imgNews;
    @BindView(R.id.text_withdraw_record)
    TextView textWithdrawRecord;
    @BindView(R.id.rv_list)
    RecyclerView rvList;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout swipeLayout;

    WithDrawListAdapter indexAdapter;

    List<RecordBean.DataBeanX.DataBean> dataBeanList;
    private int currentPage = 1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_withdraw_record);
        ButterKnife.bind(this);
        dataBeanList = new ArrayList<>();
        rvList.setLayoutManager(new LinearLayoutManager(this));
        initData();
    }

    protected void initData() {
        rvList.setItemAnimator(new DefaultItemAnimator());
        //recyclerView.setLayoutManager(new LinearLayoutManager(context));
        rvList.setLayoutManager(new LinearLayoutManager(this));

        indexAdapter = new WithDrawListAdapter(null);
        indexAdapter.openLoadAnimation(BaseQuickAdapter.SCALEIN);
        indexAdapter.isFirstOnly(false);
//        Drawable drawable = ContextCompat.getDrawable(getActivity(), R.drawable.line_divider);
//        rvList.addItemDecoration(new SimpleDividerItemDecoration(getActivity(), drawable, 1));

        rvList.setAdapter(indexAdapter);

        swipeLayout.setColorSchemeColors(Color.RED, Color.BLUE, Color.GREEN);
        swipeLayout.setOnRefreshListener(this);
        indexAdapter.setOnLoadMoreListener(this);

        //开启loading,获取数据
        setRefreshing(true);
        onRefresh();
    }


    @OnClick({R.id.img_news, R.id.text_withdraw_record})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_news:
                finish();
                break;
            case R.id.text_withdraw_record:
                break;
        }
    }

    @Override
    public void onRefresh() {
        new ConnWithDrawRecord(BaseApplication.basePreferences.readUid(), 1, new AsyCallBack<RecordBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, RecordBean s) throws Exception {
                super.onSuccess(toast, type, o, s);

                dataBeanList = s.getData().getData();
                if (dataBeanList != null) {
                    currentPage = 1;
                    indexAdapter.setNewData(dataBeanList);
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                LovelyToast.makeText(WithDrawRecordActivity.this, "网络请求失败", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                indexAdapter.removeAllFooterView();
                setRefreshing(false);
                indexAdapter.setEnableLoadMore(false);
            }
        }


        ).execute(WithDrawRecordActivity.this, false);
    }

    @Override
    public void onLoadMoreRequested() {
        new ConnWithDrawRecord(BaseApplication.basePreferences.readUid(), currentPage, new AsyCallBack<RecordBean>() {
            @Override
            public void onStart(int type) throws Exception {
                super.onStart(type);
            }

            @Override
            public void onSuccess(String toast, int type, Object o, RecordBean s) throws Exception {
                super.onSuccess(toast, type, o, s);

              /*  dataBeanList = s.getData().getData();
                if (dataBeanList != null) {
                    currentPage = 1;
                    indexAdapter.setNewData(dataBeanList);
                }*/
                dataBeanList = s.getData().getData();
                if (dataBeanList != null && dataBeanList.size() > 0) {
                    currentPage++;
                    indexAdapter.setNewData(dataBeanList);
                } else {
                    //显示没有更多数据
                    // newsAdapter.loadComplete();
                    indexAdapter.loadMoreComplete();
//                    View noDataView = inflater.inflate(R.layout.item_no_data, (ViewGroup) rvList.getParent(), false);
//                    indexAdapter.addFooterView(noDataView);
                }
            }

            @Override
            public void onFail(String toast, int type) throws Exception {
                super.onFail(toast, type);
                LovelyToast.makeText(WithDrawRecordActivity.this, "网络请求失败", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }

            @Override
            public void onEnd(String toast, int type) throws Exception {
                super.onEnd(toast, type);
                indexAdapter.setEnableLoadMore(false);
                LovelyToast.makeText(WithDrawRecordActivity.this, "没有更多数据了", 0, LovelyToast
                        .SUCCESS, LovelyToast.SCALE);
            }
        }


        ).execute(WithDrawRecordActivity.this, false);
    }

    public void setRefreshing(final boolean refreshing) {
        swipeLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeLayout.setRefreshing(refreshing);
            }
        });
    }
}
